package demo.java.java8.Lambdas.demo7;

import java.util.stream.Stream;

public class WordCounter {

	private final int counter;
	private final boolean lastSpace;
	
	public WordCounter(int count,boolean lastSpace){
		this.counter=count;
		this.lastSpace=lastSpace;
	}
	
	public WordCounter accumulate(Character c){
		if(Character.isWhitespace(c)){
			return lastSpace ? this : new WordCounter(counter, true);
		}else{
			return lastSpace ? new WordCounter(counter, false) : this;
		}
	}
	
	public WordCounter combine(WordCounter wordCounter){
		return new WordCounter(counter+wordCounter.counter, wordCounter.lastSpace);
	}
	

	
	public int getCounter(){
		return counter;
	}
}
